package com.wave.wavesystem.business.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wave.wavesystem.business.mapper.UserMapper;
import com.wave.wavesystem.business.mapper.UserRoleMapper;
import com.wave.wavesystem.business.pojo.dto.userRelation.UserRoleQueryRequest;
import com.wave.wavesystem.business.pojo.entity.UserRoleEntity;
import com.wave.wavesystem.business.pojo.entity.UserEntity;
import com.wave.wavesystem.business.service.UserRoleService;
import wave.common.pojo.entity.Query;
import com.wave.wavesystem.common.util.Condition;
import com.wave.wavesystem.common.util.Func;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-02-11
 * @description 用户-角色关联表 业务层接口
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleEntity> implements UserRoleService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean batchSave(UserRoleQueryRequest userRoles) {
        List<Long> userIds = Func.strToLongList(userRoles.getUserIds());
        if (Func.isEmpty(userIds)) {
            return true;
        }
        List<UserRoleEntity> saveUserRole = userIds.stream().
                map(item -> new UserRoleEntity(userRoles.getRoleId(), item)).toList();
        return this.saveBatch(saveUserRole);
    }

    @Override
    public boolean kickOutDept(UserRoleQueryRequest useRoles) {
        List<Long> userId = Func.strToLongList(useRoles.getUserIds());
        return this.remove(QueryChain.of(UserRoleEntity.class)
                .eq(UserRoleEntity::getRoleId, useRoles.getRoleId())
                .in(UserRoleEntity::getUserId, userId));
    }

    @Override
    public List<UserEntity> notInRoleUser(Long roleId) {
        List<UserRoleEntity> roleUsers = this.mapper.selectListByQuery(QueryChain.of(UserRoleEntity.class)
                .eq(UserRoleEntity::getRoleId, roleId));
        if (Func.isEmpty(roleUsers)) {
            return userMapper.selectAll();
        }
        List<Long> roleUserIds = roleUsers.stream().map(UserRoleEntity::getUserId).toList();
        return userMapper.selectListByQuery(QueryChain.of(userMapper)
                .notIn(UserEntity::getId, roleUserIds));
    }

    @Override
    public Page<UserEntity> roleUsersPage(Long roleId, Query query) {
        QueryChain<UserRoleEntity> queryChain = QueryChain.of(UserRoleEntity.class)
                .eq(UserRoleEntity::getRoleId, roleId);
        Page<UserRoleEntity> relationPage = this.mapper.paginate(Condition.getPage(query), queryChain);
        if (Func.isEmpty(relationPage.getRecords())) return new Page<>();
        List<Long> userIds = relationPage.getRecords().stream().map(UserRoleEntity::getUserId).toList();
        List<UserEntity> userEntities = userMapper.selectListByIds(userIds);
        if (Func.isEmpty(userEntities)) return new Page<>();
        Page<UserEntity> userPage = new Page<>(query.getPageNum(), query.getPageSize());
        userPage.setTotalRow(relationPage.getTotalRow());
        userPage.setRecords(userEntities);
        return userPage;
    }

}
